import UIKit

class XLStatusToolBar: UIView {
    
    var retweetButton: UIButton!
    var commentButton: UIButton!
    var unlikeButton: UIButton!
    
    var statusViewModel: XLStatusViewModel?{
    
        didSet {
            
            // 设置三个按钮的数据
            retweetButton.setTitle(statusViewModel?.reposts_count, for: UIControlState.normal)
            commentButton.setTitle(statusViewModel?.comments_count, for: UIControlState.normal)
            unlikeButton.setTitle(statusViewModel?.attitudes_count, for: UIControlState.normal)
            
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        // 1. 添加三个按钮
        retweetButton = addChildButton(imgNamed: "timeline_icon_retweet", defaultTitle: "转发")
        commentButton = addChildButton(imgNamed: "timeline_icon_comment", defaultTitle: "评论")
        unlikeButton = addChildButton(imgNamed: "timeline_icon_unlike", defaultTitle: "赞")
        
        // 2. 添加两个分割线
        let sp1 = UIImageView(image: #imageLiteral(resourceName: "timeline_card_bottom_line"))
        let sp2 = UIImageView(image: #imageLiteral(resourceName: "timeline_card_bottom_line"))
        addSubview(sp1);addSubview(sp2)
        
        // 3. 约束
        retweetButton.snp_makeConstraints { (make) -> Void in
            make.left.top.bottom.equalTo(self)
            make.width.equalTo(commentButton.snp_width)
        }
        
        commentButton.snp_makeConstraints { (make) in
            make.left.equalTo(retweetButton.snp_right)
            make.top.bottom.equalTo(retweetButton)
            make.width.equalTo(unlikeButton.snp_width)
        }
        unlikeButton.snp_makeConstraints { (make) in
            make.right.top.bottom.equalTo(self)
            make.left.equalTo(commentButton.snp_right)
            make.width.equalTo(retweetButton.snp_width)
        }
        
        sp1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton.snp_right)
            make.centerY.equalTo(retweetButton)
        }
        
        sp2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton.snp_right)
            make.centerY.equalTo(commentButton)
        }
    }
    
    
    
    /// 添加按钮的公共方法
    ///
    /// - parameter imgNamed: 按钮图标的名字
    ///
    /// - returns: 返回该按钮
    private func addChildButton(imgNamed: String, defaultTitle: String) -> UIButton {
        let button = UIButton()
        // 设置背景图片
        button.setBackgroundImage(UIImage(named:"timeline_card_bottom_background"), for: .normal)
        button.setBackgroundImage(UIImage(named:"timeline_card_bottom_background_highlighted"), for: .highlighted)
        // 设置左边显示的图标
        button.setImage(UIImage(named: imgNamed), for: .normal)
        // 设置标题
        button.setTitle(defaultTitle, for: UIControlState.normal)
        button.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        addSubview(button)
        
        return button
    }
    
}
